
package com.emser.unc.client;

import com.emser.unc.client.event.LoginEvent;
import com.emser.unc.client.helper.RPCCall;
import com.emser.unc.client.presenter.LoginPresenter;
import com.emser.unc.client.service.LoginService;
import com.emser.unc.client.service.LoginServiceAsync;
import com.emser.unc.client.view.LoginView;
import com.emser.unc.serializable.UserAccountDTO;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.maps.gwt.client.GoogleMap;
import com.google.maps.gwt.client.Marker;

public class CloudMobile implements EntryPoint {
	private static CloudMobile singleton;
	private SimpleEventBus eventBus;
	private LoginServiceAsync rpcService;
	private RootPanel rootPanel;
	private DockPanel dockPanel;
	private SimplePanel northPanel,westPanel;
	private SimplePanel centerPanel;
	private UserAccountDTO userA;
	private SimplePanel mapPanel;
	private Marker marker = null;
	private GoogleMap theMap = null;
	public static AppConstants constants;
	private Timer t = null;
	private int heightClientWindow = Window.getClientHeight();
	private int heightHeaderPanel = 90;
	private int heightPanelCenter = heightClientWindow - heightHeaderPanel;

	public static CloudMobile get() {
	    return singleton;
	  }
	
	private void initPanel(){
		dockPanel = new DockPanel();
		dockPanel.setSize("100%", heightClientWindow +"px");
		northPanel = new SimplePanel();
		northPanel.setSize("100%", "100%");
		westPanel = new SimplePanel();
		westPanel.setSize("100%", "100%");
		centerPanel = new SimplePanel();
		centerPanel.setSize("100%", "100%");
		dockPanel.add(northPanel, DockPanel.NORTH);
		dockPanel.setCellHeight(northPanel, heightHeaderPanel+"px");
		dockPanel.add(westPanel, DockPanel.WEST);
		dockPanel.setCellVerticalAlignment(westPanel, HasVerticalAlignment.ALIGN_MIDDLE);
		dockPanel.setCellHorizontalAlignment(westPanel, HasHorizontalAlignment.ALIGN_CENTER);
		dockPanel.setCellHeight(westPanel, heightPanelCenter + "px");
		dockPanel.setCellWidth(westPanel, "200px");	
		dockPanel.add(centerPanel,DockPanel.CENTER);
		dockPanel.setCellVerticalAlignment(centerPanel, HasVerticalAlignment.ALIGN_MIDDLE);
		dockPanel.setCellHorizontalAlignment(centerPanel, HasHorizontalAlignment.ALIGN_CENTER);
		dockPanel.setCellHeight(centerPanel, heightPanelCenter + "px");
		dockPanel.setCellWidth(centerPanel,"100%-200px");
		rootPanel.add(dockPanel);
	}
	
	public void onModuleLoad() {
		singleton = this;
		rpcService = GWT.create(LoginService.class);
		constants = GWT.create(AppConstants.class);
		eventBus = new SimpleEventBus();
		rootPanel = RootPanel.get();
		getLoggedInUser(); 
	}
	
	private void getLoggedInUser() {
	    new RPCCall<UserAccountDTO>() {
	      @Override
	      protected void callService(AsyncCallback<UserAccountDTO> cb) {
	        rpcService.getLoggedInUserDTO(cb);
	      }
	      @Override
	      public void onSuccess(UserAccountDTO loggedInUserDTO) {
	        if (loggedInUserDTO == null) {
	          showLoginView();
	        } else {
	          setCurrentUser(loggedInUserDTO);
	          createUI();
	        }
	      }
	      @Override
	      public void onFailure(Throwable caught) {
	        Window.alert("Error: " + caught.getMessage());
	      }
	    }.retry(3);
	  }

	  private void createUI() {
		    GWT.runAsync(new RunAsyncCallback() {
		      @Override
		      public void onFailure(Throwable reason) {
		        Window.alert("Code download error: " + reason.getMessage());
		      }
		      @Override
		      public void onSuccess() {
		        goAfterLogin();
		        eventBus.fireEvent(new LoginEvent(userA));
		      }
		    });
		  }
	  
	public void showLoginView() {
	    rootPanel.clear();
	    LoginPresenter loginPresenter = new LoginPresenter(eventBus, new LoginView());
	    loginPresenter.go(rootPanel);
	}
	
	private void goAfterLogin() {
	    rootPanel.clear();
	    initPanel();
	    AppController appViewer = new AppController(eventBus);
	    appViewer.setContainers(northPanel, westPanel, centerPanel);
	    appViewer.go(rootPanel);  
	}
	
	public SimpleEventBus getEventBus() {
		    return eventBus;
	}
	
	private void setCurrentUser(UserAccountDTO currentUser) {
		 this.userA = currentUser;
	}
	
	public UserAccountDTO getCurrentUser(){
		 return this.userA;
	}

	public void setPanelMap(SimplePanel panelMap) {
		this.mapPanel = panelMap;
	}

	public SimplePanel getPanelMap() {
		return mapPanel;
	}

	public void setMarker(Marker marker) {
		this.marker = marker;
	}

	public Marker getMarker() {
		return marker;
	}

	public void setTheMap(GoogleMap theMap) {
		this.theMap = theMap;
	}

	public GoogleMap getTheMap() {
		return theMap;
	}

	public void setT(Timer t) {
		this.t = t;
	}

	public Timer getT() {
		return t;
	}
	public int getHeightClientWindow() {
		return heightClientWindow;
	}

	public int getHeightHeaderPanel() {
		return heightHeaderPanel;
	}

	public int getHeightPanelCenter() {
		return heightPanelCenter;
	}
}